// pages/restaurant/cook/moreCook/moreCook.js
const util = require('../../../utils/util');
const app = getApp();
import {
  list
} from '../../../api/chef'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cookInfos: [],
    name: '',
    pageNum: 1,
    APP_OSSURL_API: app.APP_OSSURL_API,
    cookInfoBgImage: "/dinnerBar/canyin/canyin-chushibeij.png",
    cookPortraitImage: "/dinnerBar/canyin/gengduochushi/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201223091648.png",
    dishImage: "/dinnerBar/canyin/gengduochushi/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20201223091513.png",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getChefList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageNum += 1
    this.getChefList('')

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 跳转到厨师详情页
   */
  toCookDetail: function (e) {
    wx.navigateTo({
      url: `../cookDetail/cookDetail?cookId=${e.currentTarget.dataset.id}`,
    })
  },
  bindconfirm(val) {
    this.data.cookInfos = []
    this.data.pageNum = 1
    this.data.name = val.detail.value
    this.getChefList()
  },
  bindinput(val) {
    this.data.name = val.detail.value
    if (val.detail.value == '') {
      this.data.pageNum = 1
      this.data.cookInfos = []
      this.getChefList()
    }
  },
  /**
   * 获取厨师列表
   * @param {String} keyword 查询关键词
   */
  getChefList: function () {
    list({
      name: this.data.name,
      pageNum: this.data.pageNum
    }).then(res => {
      res.data.data.forEach(item => {
        item.starts = []
        item.meta = JSON.parse(item.meta)
        item.avatar = util.thumbnail(item.avatar, 256, 336)
        this.data.cookInfos.push(item)
      })

      this.setData({
        cookInfos: this.data.cookInfos
      });
    });
  }
})